<?php
namespace Api\Controller;

use Think\Controller;

class WechatController extends Controller
{
    protected $appid ='wx19a7a6bb388bad14';
    protected $secret ='96b926f97c2e83f8eccb0375b0cca24e';
     
    /**
     * 微信服务器配置 - 验签
     */
    public function index()
    {
        
    
        $signature = $_GET["signature"];
        $timestamp = $_GET["timestamp"];
        $nonce = $_GET["nonce"];
        $echostr=$_GET['echostr'];
        $this->show($echostr);
        
        // $token = '8c52470b71cde0780b94c98693db773b';
        // $tmpArr = array($token, $timestamp, $nonce);
        // sort($tmpArr, SORT_STRING);
        // $tmpStr = implode( $tmpArr );
        // $tmpStr = sha1( $tmpStr );
        
        // if( $tmpStr == $signature ){
        //     return true;
        // }else{
        //     return false;
        // }
 
    }



    /**
     * 返回：
        {
            "access_token":"ACCESS_TOKEN",
            "expires_in":7200
        }

 
     */
    public function access_token(){ 
        
        $sname = 'access_token';//缓存的名字
        if(S($sname)){
            $rs = S($sname);
        }else{
            $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$this->appid}&secret={$this->secret}";

            $rs = file_get_contents($url);
            $rs = \json_decode($rs,true);
            if($rs['access_token']){
                S($sname,$rs,7000);
            }
        }
       
        //var_dump($rs);
        return $rs;

    }


    /**
     * js api  需要用到
     * 
     * {
    "errcode":0,
    "errmsg":"ok",
    "ticket":"bxLdikRXVbTPdHSM05e5u5sUoXNKd8-41ZO3MhKoyN5OfkWITDGgnr2fwJ0m9E8NYzWKVZvdVtaUgWvsdshFKA",
    "expires_in":7200
    }
     */
    public function jsapi_ticket(){
        $sname = 'jsapi_ticket';//缓存的名字
        if(S($sname)){
            $rs = S($sname);
        }else{
            $access_token = $this->access_token();
            $access_token = $access_token['access_token'];
            $rs = file_get_contents("https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token={$access_token}&type=jsapi");
            $rs = \json_decode($rs,true);
            if($rs['errcode']==0){
                S($sname,$rs,7000);
            }
        }
       
        //var_dump($rs);
        return $rs;
    }
    


    /**
     * 分享接口的config 签名
     */
    public function shareSign(){
        
        $jsapi_ticket= $this->jsapi_ticket();
        $url='http://'.$_SERVER['SERVER_NAME'].$_SERVER["REQUEST_URI"]; 
        $url =I('param.url','');//js传过来
        $config = [
            'noncestr'=>'Wm3WZYTPz0wzccnW',
            'jsapi_ticket'=>$jsapi_ticket['ticket'],
            'timestamp'=>time(),
            'url'=>$url,
        ];

        ksort($config);
        $signstr = urldecode(http_build_query($config));
        $sign = sha1($signstr);

        // var_dump($config);
        // var_dump($signstr);
        // var_dump($sign);

        $resdata = $config;
        $resdata['signature'] = $sign;
        $resdata['appid'] = $this->appid; 
        $resdata['code'] = 200;
        $resdata['msg'] = '获取成功';


        $resdata['title']="全国人口普查条例知识竞答";
        $resdata['desc']="《全国人口普查条例》相关知识竞赛，长知识、赢奖品，快来参与吧！";
        $resdata['link']=$url;//"http://pucha.getpv.com/index.html";//"http://t.cn/A62Llo95";
        $resdata['imgUrl']="http://pucha.getpv.com/images/share_logo.jpg";

        echo return_ajax($resdata);



    }



}